<template>
  <div class="check-in-manage">
    <div class="header">
      <el-select v-model="selectedCourseId" placeholder="按课程筛选" clearable @change="fetchFilteredCheckIns">
        <el-option
          v-for="course in courseStore.courses"
          :key="course.id"
          :label="course.title"
          :value="course.id"
        />
      </el-select>
    </div>

    <el-card class="stats-card">
      <!-- <div class="stats-header">
        <h3>签到统计</h3>
      </div> -->
      <div class="chart-container" ref="chartRef"></div>
    </el-card>

    <el-card class="records-card">
      <div class="records-header">
        <h3>签到记录</h3>
      </div>
      <el-table :data="checkInStore.checkIns" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="courseTitle" label="课程" ></el-table-column>
        <el-table-column prop="studentName" label="学生" ></el-table-column>
        <el-table-column prop="checkInTime" label="签到时间" ></el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button
              type="danger"
              size="small"
              @click="checkInStore.deleteCheckIn(row.id)"
              :icon="Delete"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue';
import { Delete } from '@element-plus/icons-vue';
import * as echarts from 'echarts';
import { ElMessage } from 'element-plus';
import { useCheckInStore } from '@/stores/api/check-in';
import { useCourseStore } from '@/stores/api/course';

const checkInStore = useCheckInStore();
const courseStore = useCourseStore();

const selectedCourseId = ref<number | null>(null);
const chartRef = ref<HTMLElement | null>(null);
let chartInstance: echarts.ECharts | null = null;

const fetchFilteredCheckIns = async () => {
  try {
    if (selectedCourseId.value) {
      await checkInStore.fetchCheckInsByCourse(selectedCourseId.value);
    } else {
      checkInStore.checkIns = [];
    }
  } catch (error) {
    ElMessage.error('获取签到记录失败');
  }
};

const initChart = async () => {
  if (!chartRef.value) return;
  chartInstance = echarts.init(chartRef.value);

  if (!selectedCourseId.value) {
    chartInstance.setOption({
      title: { text: '请先选择课程' },
      xAxis: { type: 'category', data: [] },
      yAxis: { type: 'value' },
      series: [{ type: 'bar', data: [] }],
    });
    return;
  }

  try {
    const stats = await checkInStore.fetchStatsByCourse(selectedCourseId.value);
    const xData = stats.map(item => item.studentName);
    const yData = stats.map(item => item.checkInRate.toFixed(2));

    chartInstance.setOption({
      title: { text: '签到率统计' },
      tooltip: { trigger: 'axis' },
      xAxis: { type: 'category', data: xData },
      yAxis: { type: 'value', name: '签到率 (%)' },
      series: [
        {
          type: 'bar',
          data: yData,
          itemStyle: { color: '#409EFF' },
        },
      ],
    });
  } catch (error) {
    ElMessage.error('获取统计数据失败');
  }
};

watch(selectedCourseId, () => {
  fetchFilteredCheckIns();
  initChart();
});

onMounted(async () => {
  await courseStore.fetchCourses();
  if (courseStore.courses.length > 0) {
    selectedCourseId.value = courseStore.courses[0].id
  }
  initChart();
});
</script>

<style scoped>
.check-in-manage {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header h2 {
  font-size: 24px;
  color: #333;
}

.header .el-select {
  width: 200px;
}

.stats-card,
.records-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.stats-header h3,
.records-header h3 {
  font-size: 18px;
  color: #333;
  margin-bottom: 10px;
}

.chart-container {
  height: 400px;
}

.el-table {
  border-radius: 8px;
}
</style>